//
//  TipsFactory.m
//  carcareIOS
//
//  Created by ileo on 15/11/13.
//  Copyright © 2015年 baozun. All rights reserved.
//

#import "TipsFactory.h"

typedef NS_ENUM(NSInteger, TAG_TIPS){
    TAG_ADD = 19,
};

@implementation TipsFactory

+(void)addAddTipsOnView:(UIView *)view tips:(NSString *)tips click:(void (^)())click{
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 205 * __MAIN_RATIO_375, 150 * __MAIN_RATIO_375)];
    imageView.center = CGPointMake(view.width / 2, view.height / 3);
    [view addSubview:imageView];
    imageView.tag = TAG_ADD;

    UIGraphicsBeginImageContext(imageView.frame.size);   //开始画线
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);  //设置线条终点形状
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, COLOR_HEXSTRING(@"#dcdcdc").CGColor);
    CGContextSetStrokeColorWithColor(context, COLOR_HEXSTRING(@"#dcdcdc").CGColor);
    
    CGFloat lengths[] = {8,5};
    CGContextSetLineDash(context, 0, lengths, 2);  //画虚线
    
    CGFloat radius = 4;
    CGFloat gap = 2;
    
    CGContextMoveToPoint(context, radius + gap, gap);
    CGContextAddLineToPoint(context, imageView.width - radius - gap, gap);
    CGContextAddArcToPoint(context, imageView.width - gap, gap, imageView.width - gap, gap + radius, radius);
    CGContextAddLineToPoint(context, imageView.width - gap, imageView.height - gap - radius);
    CGContextAddArcToPoint(context, imageView.width - gap, imageView.height - gap, imageView.width - radius - gap, imageView.height - gap, radius);
    CGContextAddLineToPoint(context, gap + radius, imageView.height - gap);
    CGContextAddArcToPoint(context, gap, imageView.height - gap, gap, imageView.height - gap - radius, radius);
    CGContextAddLineToPoint(context, gap, radius + gap);
    CGContextAddArcToPoint(context, gap, gap, gap + radius, gap, radius);
    
    CGContextStrokePath(context);
    
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    
    CGFloat longOne = 60;
    CGFloat shortOne = 8;
    
    UIView *horV = [[[UIView viewWithFrame:CGRectMake(0, 0, longOne, shortOne)] resetBackgroundColor:COLOR_HEXSTRING(@"#dcdcdc")] setupOnView:imageView];
    UIView *verV = [[[UIView viewWithFrame:CGRectMake(0, 0, shortOne, longOne)] resetBackgroundColor:COLOR_HEXSTRING(@"#dcdcdc")] setupOnView:imageView];
    horV.layer.cornerRadius = 2;
    verV.layer.cornerRadius = 2;
    horV.center = imageView.boundsCenter;
    verV.center = imageView.boundsCenter;
    
    [Factory labelWithFrame:CGRectMake(0, imageView.bottom + 15, __MAIN_WIDTH, 20) font:FONT_DEFAULT_Light(15) text:tips textColor:COLOR_HEXSTRING(@"#dcdcdc") onView:view textAlignment:NSTextAlignmentCenter].tag = TAG_ADD;
    
    [[UIButton buttonEmptyWithFrame:imageView.frame click:click] setupOnView:view].tag = TAG_ADD;
}

+(void)removeAddTipsOnView:(UIView *)view{
    for (UIView *aView in view.subviews) {
        if (aView.tag == TAG_ADD) {
            [aView removeFromSuperview];
        }
    }
}

@end
